﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Runtime.Serialization;
using System.Xml;
using System.IO;

namespace kb_24_the_game
{
    internal class Level1
    {
        #region Variabelen Declaractie
        public bool reset = false;
        private Rectangle oldColliderRectangle;
        public string backgroundNaam { get; set; }
        private ContentManager content;
        private Rectangle spelerRect;
        KeyboardState state;
        private Backgrounds scrollBackground1;
        private Player speler;
        private Biertje biertje;
        private ObstakelBal bowlingbal;
        XML_IO xml = new XML_IO();
        private IList<ISprite> allSprites;
        private List<ObstakelTest> obstakellijst;
        private List<ObstakelTest> trapLijst = new List<ObstakelTest>();
        private List<ObstakelTest> kegelLijst = new List<ObstakelTest>();
        private SpriteFont font, fontreset;
        private bool bovencollision = false, zijcollisionn = false, blockRechts = false, blockLinks = false, up = false, gatGevallen = false;
        #endregion Variabelen Declaractie

        public Level1(ContentManager content)
        {
            this.content = content;
            
            allSprites = new List<ISprite>();
            
            speler = new Player(content, new Vector2(51, 425));

            obstakellijst = xml.readXML(@"XML/level1.xml");

            foreach (ObstakelTest obstakel in obstakellijst)
            {
                obstakel.content = content;
            }

            bowlingbal = new ObstakelBal(content, new Vector2(700, 470), "Level1Textures/BowlingBallAnimation");
            
            biertje = new Biertje(content, new Vector2(150, 400), 1);
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            fontreset = content.Load<SpriteFont>("AlgemeneTextures/fontReset");
            Load();
        }

        public void Load()
        {
            scrollBackground1 = new Backgrounds(content.Load<Texture2D>("Achtergronden/BackgroundLevel01"), new Rectangle(0, 0, 3800, 600));

            allSprites.Add(scrollBackground1);
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                allSprites.Add(obstakel);
            }
            allSprites.Add(speler);
            allSprites.Add(bowlingbal);
            allSprites.Add(biertje);

            trapLijst.Add(obstakellijst[8]);
            trapLijst.Add(obstakellijst[9]);

            for (int i = 1; i <= 7; i++)
            {
                kegelLijst.Add(obstakellijst[i]);
            }
        }

        public bool Update(GameTime gameTime)
        {
            spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, speler.texture.Width / 4, speler.texture.Height / 2);
            foreach (ObstakelTest obstakel in obstakellijst)
            {
                obstakel.Update(gameTime);
            }
            speler.Springen(up);
            if (obstakellijst[0].colliderRect != null)
            {
                CheckCollisionsMetTrap(spelerRect);
                CheckColisionBowlingbalTrap();
                CheckCollisionsSpelerBowlingbal(spelerRect);
                CheckCollisionsMetBiertjes(spelerRect);
                CheckCollisionMetPut(spelerRect);
                CheckCollisionTrapMetBiertje();
                CheckCollsionsSpelerKegel(spelerRect);
            }
            CheckReset();
            CheckGevallen();
            
            bowlingbal.SetSpelerDirectie(speler.loopDirectie);
            biertje.CheckPositionsSpelerBiertje(speler.playerPosition);
            bowlingbal.Update(gameTime);
            speler.Update(gameTime);
            foreach (ObstakelTest k in kegelLijst)
            {
                k.ValObstakel(scrollBackground1.rectangle.X);
            }
            if (obstakellijst[0].colliderRect != null)
            {
                if (biertje.aantalBiertjes >= 5)
                {
                    obstakellijst[10].position = new Vector2(scrollBackground1.rectangle.X + 3400, 470);
                }
                else
                {
                    obstakellijst[10].position = new Vector2(scrollBackground1.rectangle.X + 3400, -500);
                }
            }
            bool einde = CheckEindItem(spelerRect);



            CheckKeys();
            return einde;

        }

        

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (ISprite sprite in allSprites)
            {
                sprite.Draw(spriteBatch);
            }
             if (gatGevallen == true)
            {                
                spriteBatch.DrawString(fontreset, "Je bent gevallen!", new Vector2(125, 200), Color.Red);
                                                    
                spriteBatch.DrawString(font,"Druk op spate om opnieuw te beginnen", new Vector2(175, 300), Color.White);
            }
        }

        private void CheckKeys()
        {
            state = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Z))
            {
                biertje.aantalBiertjes = 5;
            }
            if (!gatGevallen)
            {
                if (state.IsKeyDown(Keys.Up))
                {
                    up = true;
                }
                else
                {
                    up = false;
                }
                if (state.IsKeyDown(Keys.Left) && !blockRechts)
                {
                    if (speler.playerPosition.Y <= 430)
                    {
                        if (scrollBackground1.rectangle.X <= -1)
                        {
                            scrollBackground1.Links();
                            biertje.BeweegLinks();
                            foreach (ObstakelTest obstakel in obstakellijst)
                            {
                                obstakel.BeweegLinks();
                            }
                        }
                        if (speler.playerPosition.X >= 50)
                        {
                            speler.Links();
                        }
                    }
                }
                if (state.IsKeyDown(Keys.Right) && !blockLinks)
                {
                    if (speler.playerPosition.Y <= 430)
                    {
                        if (scrollBackground1.rectangle.X >= -2999)
                        {
                            scrollBackground1.Rechts();
                            biertje.BeweegRechts();
                            foreach (ObstakelTest obstakel in obstakellijst)
                            {
                                obstakel.BeweegRechts();
                            }
                        }
                        if (speler.playerPosition.X <= 500)
                        {
                            speler.Rechts();
                        }
                    }
                }
            }
        }
        public void CheckReset()
        {
            if (state.IsKeyDown(Keys.Space) && gatGevallen == true)
            {
                reset = true;
            }
        }
        public void CheckGevallen()
        {
            if (speler.playerPosition.Y >= 430)
            {
                gatGevallen = true;
            }
        }
        public bool CheckEindItem(Rectangle spelerRect)
        {
            Rectangle portemonee = new Rectangle((int)obstakellijst[10].position.X, (int)obstakellijst[10].position.Y, obstakellijst[10].texture.Width, obstakellijst[10].texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, portemonee);
            if (!overlap.IsEmpty)
            {
                return true;
            }
            return false;
        }

        private void CheckCollisionsMetBiertjes(Rectangle spelerRect)
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bier);
            if (overlap.IsEmpty == false)
            {
                biertje.Geraakt(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
            }
        }

        private void CheckCollisionTrapMetBiertje()
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            foreach (ObstakelTest o in trapLijst)
            {
                foreach (Rectangle r in o.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(r, bier);
                    if (!overlap.IsEmpty)
                    {
                        biertje.Geraakt(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                    }
                }
            }
        }

        public void CheckColisionBowlingbalTrap()
        {
            Rectangle bowlingball = new Rectangle((int)bowlingbal.ballPosition.X, (int)bowlingbal.ballPosition.Y, bowlingbal.texture.Width / 8, bowlingbal.texture.Height / 2);
            foreach (ObstakelTest o in trapLijst)
            {
                foreach (Rectangle r in o.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(r, bowlingball);
                    if (!overlap.IsEmpty && oldColliderRectangle.IsEmpty)
                    {
                        bowlingbal.bounce =true;
                        oldColliderRectangle = overlap;
                        return;
                        
                    }
                    else
                    {
                        bowlingbal.bounce = false;
                    }
                    
                }
            }
        }

        public void CheckCollisionsSpelerBowlingbal(Rectangle spelerRect)
        {
            Rectangle bowlingball = new Rectangle((int)bowlingbal.ballPosition.X, (int)bowlingbal.ballPosition.Y, bowlingbal.texture.Width / 8, bowlingbal.texture.Height / 2);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bowlingball);
            if (!overlap.IsEmpty && oldColliderRectangle.IsEmpty)
            {
                biertje.ResetBiertje(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
            }
            oldColliderRectangle = overlap;
        }

        public void CheckCollisionMetPut(Rectangle spelerrect)
        {
            foreach (Rectangle r in obstakellijst[0].colliderRect)
            {
                Rectangle overlap = Rectangle.Intersect(spelerrect, r);
                if (!overlap.IsEmpty)
                {
                    if (overlap.Width >= spelerrect.Width)
                    {
                        speler.grondhoogte = 600;
                    }
                }
            }
        }

        public void CheckCollisionsMetTrap(Rectangle spelerRect)
        {
            bovencollision = false;
            zijcollisionn = false;

            foreach (ObstakelTest o in trapLijst)
            {
                foreach (Rectangle r in o.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(spelerRect, r);

                    if (!overlap.IsEmpty && overlap.Width > overlap.Height && overlap.Width > 8)
                    {
                        //collision
                        speler.grondhoogte = (int)speler.playerPosition.Y;
                        bovencollision = true;
                        continue;
                    }
                    else
                    {
                        if (!bovencollision)
                        {
                            speler.grondhoogte = 425;
                        }
                    }
                    if (!overlap.IsEmpty && overlap.Height > overlap.Width)
                    {
                        if (spelerRect.X < r.X)
                        {
                            //linker kant
                            speler.magRechts = false;
                            speler.magLinks = true;
                            blockLinks = true;
                            blockRechts = false;
                            zijcollisionn = true;
                        }
                        else if (spelerRect.X > r.X)
                        {
                            //rechterkant
                            speler.magLinks = false;
                            speler.magRechts = true;
                            blockLinks = false;
                            blockRechts = true;
                            zijcollisionn = true;
                        }
                    }
                    else if (overlap.IsEmpty && zijcollisionn == false && bovencollision == false)
                    {
                        speler.magLinks = true;
                        speler.magRechts = true;
                        blockLinks = false;
                        blockRechts = false;
                    }
                }
            }
        }
        
        public void CheckCollsionsSpelerKegel(Rectangle spelerRect)
        {
            foreach (ObstakelTest k in kegelLijst)
            {
                foreach (Rectangle r in k.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(r, spelerRect);
                    if (!overlap.IsEmpty)
                    {
                        biertje.ResetBiertje(scrollBackground1.rectangle.X, scrollBackground1.rectangle.Width);
                        k.ResetVal(scrollBackground1.rectangle.X);
                    }
                }
            }
        }
    }
}